home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power Programmierung
/
Power-Programmierung (Tewi)(1994).iso
/
magazine
/
comp_lan
/
85_febr
/
argpar.1
< prev
next >
Wrap
Text File
|
1988-07-25
|
3KB
|
102 lines
Listing 1
to
Macros Special Series on C Macros
February 1985
>> Simplified argument parsing, positional and flag <<
#define ArgBEGIN(A_argc, A_argv) \
{ /* Begin block with local variables. Ends with ArgEND. */ \
struct \
{ int argc, *ptrArgCount; \
char ** argVector; \
char * (*ptrFunction) (); \
char keyTrigger, *keyPointer; \
int argIndex, charIndex, nextPositional, keysTerminated; \
} a0; \
a0.ptrArgCount = &(A_argc); a0.argVector = (A_argv); \
a0.argc = *a0.ptrArgCount; a0.keyTrigger = '-'; \
a0.ptrFunction = argPos;
#define ArgPosCall(A_Function) a0.ptrFunction = (A_Function);
#define ArgTrigger(A_Trigger) a0.keyTrigger = (A_Trigger);
#define ArgLOOP \
a0.keysTerminated = 0; a0.nextPositional = 1; \
for ( (a0.argIndex = 1); (a0.argIndex < a0.argc); (++a0.argIndex) ) \
{ /* for all arguments */ \
if ( ( a0.argVector[a0.argIndex][0] != a0.keyTrigger ) \
|| ( a0.keysTerminated != 0 ) \
|| ( a0.argVector[a0.argIndex][1] == '\0' ) ) \
{ /* not a key: bubble up positional arguments */ \
a0.argVector[a0.nextPositional] = a0.argVector[a0.argIndex]; \
(void) (*a0.ptrFunction) ( '\0', a0.argVector[a0.nextPositional] ); \
++a0.nextPositional; \
} \
else /* it is a key */ \
{ \
for \
( (a0.charIndex = 1, a0.keyPointer = & a0.argVector[a0.argIndex][1]) \
; ( ( a0.keyPointer != (char *) 0 ) \
&& ( (*a0.keyPointer) != '\0' ) \
) \
; (++a0.charIndex, ++a0.keyPointer) \
) \
{ /* for all characters in an argument */ \
switch ( a0.keyPointer[0] ) \
{
#define ArgFlagSet(A_KeyLetter, A_FlagCounter) \
case A_KeyLetter: ++A_FlagCounter; break;
#define ArgFlagCall(A_KeyLetter, A_Function) \
case A_KeyLetter: (A_Function)( *a0.keyPointer, 0 ); break;
#define ArgEND \
default: \
(void) fprintf \è ( stderr \
, " Stop. Key letter \'%c\' rejected.\n" \
, *a0.keyPointer \
); \
exit (1); \
} /* switch */ \
} /* for all characters in argument */ \
} /* if key or not a key */ \
} /* for all arguments */ \
(* a0.ptrArgCount) = a0.nextPositional - 1; \
} /* End block begun with ArgBEGIN. */ \
static char * argPos( a_keyChar, a_argString )
char a_keyChar, *a_argString;
{ return ( (char *) 0 ); }
/* --------------------------------------------------------------- */
#if defined DriverH
#include <stdio.h>
void main(argCount, argVector)
int argCount;
char * argVector[];
{
int f_flag = 0;
ArgBEGIN (argCount, argVector)
ArgPosCall (argPos)
ArgTrigger ('-')
ArgLOOP
ArgFlagSet ('f', f_flag)
ArgFlagCall ('F', argPos)
ArgEND
exit (0);
} /* main */
#endif DriverH